package com.by.service.impl;

import com.by.common.dao.MyBaseServiceImpl;
import com.by.common.dao.bean.PageResult;
import com.by.entity.*;
import com.by.mapper.*;
import com.by.query.ProductQuery;
import com.by.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;

import static com.by.common.core.utils.Assert.IS_FAILL;

@Service
public  class SpuServiceImpl extends MyBaseServiceImpl<Spu, SpuMapper> implements SpuService {

    @Autowired
    SpuDetailMapper spuDetailMapper; //商品详情

    @Autowired
    SpuExtensionMapper spuExtensionMapper;//商品扩展

    @Autowired
    SkuMapper skuMapper;//

    @Autowired
    SkuStockMapper skuStockMapper;

    @Autowired
    AttMapper attMapper;

    @Autowired
    AttValueMapper attValueMapper;

    @Autowired
    SpuAttrValueMapper spuAttrValueMapper;

    @Autowired
    SpuSkuAttrValueMapper spuSkuAttrValueMapper;

    @Transactional
    @Override
    public int saveEntity(Spu entity) {
        //保存产品信息
        int rs = super.saveEntity(entity);
        IS_FAILL(rs, "Spu保存失败");
        //保存产品详情
        SpuDetail detail = new SpuDetail();
        detail.setSpu_id(entity.getSpu_id());
        detail.setDetail(entity.getDetail());
        rs = spuDetailMapper.insert(detail);
        IS_FAILL(rs, "保存商品详情失败");

        //保存spu扩展
        int total = 0;//总库存量
        if (null != entity.getSkuList() && entity.getSkuList().size() > 0) {
            Iterator<Sku> it = entity.getSkuList().iterator();
            while (it.hasNext()) {
                Sku sku = it.next();
                total += sku.getActual_stock();//各sku库存量累加
                //保存sku
                //设置产品id
                sku.setSpu_id(entity.getSpu_id());
                rs = skuMapper.insert(sku);
                IS_FAILL(rs, "保存sku失败");
                //保存sku库存
                SkuStock skuStock = new SkuStock();
                skuStock.setActual_stock(sku.getActual_stock());
                skuStock.setSku_id(sku.getSku_id());
                rs = skuStockMapper.insert(skuStock);
                IS_FAILL(rs, "保存sku库存失败");
            }
        }
        SpuExtension extension = new SpuExtension();
        extension.setSpu_id(entity.getSpu_id());
        extension.setActual_stock(total);
        rs = spuExtensionMapper.insert(extension);
        IS_FAILL(rs, "商品扩展保存失败");

        //保存规格属性
        if (null != entity.getAttList() && entity.getAttList().size() > 0) {
            //遍历规格属性
            entity.getAttList().forEach(el -> {
                //保存
                int tmp = attMapper.insert(el);
                IS_FAILL(tmp, "保存规格属性失败");
                //保存规格值
                if (null != el.getAttValueList() && el.getAttValueList().size() > 0) {
                    tmp = attValueMapper.batchSave(el.getAttr_id(), el.getAttValueList());
                    IS_FAILL(tmp, "保存规格属性值失败");
                }

                //保存商品和规格属性 属性值之间的关系
                tmp = spuAttrValueMapper.save(entity.getSpu_id(), el.getAttr_id());
                IS_FAILL(tmp, "保存商品规格属性值关系失败");

                //保存 商品  规格   sku关系
                tmp=spuSkuAttrValueMapper.save(entity.getSpu_id());
                IS_FAILL(tmp, "保存商品规格sku属性值关系失败");

            });
        }

        return 1;
    }

    @Override
    public Map getDetail(Serializable id) {
        return baseMapper.getDetail(id);
    }


}
